﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySkiPatrolEntity;
using MySkiPatrolDAL;

namespace MySkiPatrolBLL
{
    public class SignInBLO
    {
        SignInDAO DAL = new SignInDAO();

        public SignInBLO()
        {

        }

        public List<ValueKeyPair> GetAppSettings(Patrol data)
        {
            List<ValueKeyPair> myReturn = new List<ValueKeyPair>();

            //first, lets return the patrol information
            PatrolDAO pDAL = new PatrolDAO();
            data = pDAL.GetPatrolByID(data);

            myReturn.Add(new ValueKeyPair("PatrolName", data.Name));
            myReturn.Add(new ValueKeyPair("PatrolTitle", data.Title));

            return myReturn;
        }

        public List<Member> GetMembersWithNoID(Patrol data)
        {
            return DAL.GetEmployeesWithNoID(data);
        }

        //public void CancelSignin(ScanResponse data)
        //{
        //    DAL.CancelSignin(data);
        //}

        public ScanResponse ProcessScan(ScanRequest data)
        {
            ScanResponse myReturn = new ScanResponse();
            myReturn.DataTransaction = data.DataTransaction;

            myReturn.ScanDateTime = data.ScanDateTime;
            myReturn.IDCardString = data.IDCardString;

            //get the employee information
            //int n;
            //bool isNumeric = int.TryParse(data.EmployeeID, out n);
            //if (isNumeric)
            //{
            //    myReturn = DAL.GetEmployeeNSPInfo(myReturn);
            //}
            //else
            //{
                myReturn = DAL.GetEmployeeIDInfo(myReturn);
            //}

                if (string.IsNullOrEmpty(myReturn.MemberID))
            {
                //the scanned id is not recorded...
                myReturn.AssignEmployeeIDCard = true;
            }
            else
            {
                //check if the patroller has signed in...
                myReturn = DAL.GetEmployeeDailySignin(myReturn);
                if (myReturn.IsSignedIn)
                {
                    //signout...
                    myReturn.SignOut = myReturn.ScanDateTime;
                    EmployeeSignOut(myReturn);
                    myReturn = null;
                }
                else
                {
                    //signin...
                    myReturn.SignIn = myReturn.ScanDateTime;
                    myReturn = DAL.EmployeeSignin(myReturn);
                    //update the last used for the id card
                    DAL.SetIDCardLastUsedDate(myReturn);
                }
            }
            return myReturn;
        }

        public List<ScanResponse> AssignIDCardToMember(ScanResponse data)
        {
            if (data.AssignEmployeeIDCard)
            {
                //assign the scanned ID to the person and signin...
                DAL.AssignIdToMember(data);
                
            }

            data.SignIn = data.ScanDateTime;
            DAL.EmployeeSignin(data);

            Patrol patrol = new Patrol();
            patrol.dbPrefix = data.DataTransaction.PatrolPrefix;
            return this.GetDailySignins(patrol);
        }

        public List<ScanResponse> GetDailySignins(Patrol data)
        {
            return DAL.GetDailySignins(data);
        }

        //public List<ScanResponse> UpdatePatrollerAssignment(ScanResponse data)
        //{
        //    if (data.AssignEmployeeID)
        //    {
        //        //assign the scanned ID to the Member and signin...
        //        DAL.AssignIdToPatroller(data);
        //        DAL.MemberSignin(data);
        //    }

        //    DAL.UpdatePatrollerAssignment(data);
        //    return this.GetDailySignins();
        //}

        public void EmployeeSignOut(ScanResponse data)
        {
            //create a new membersignin object and populate it...
            EmployeeSignIn signIn = new EmployeeSignIn();
            signIn.MemberID = data.MemberID;
            signIn.SignIn = data.SignIn;
            signIn.SignOut = data.ScanDateTime;
            signIn.DataTransaction = data.DataTransaction;
            
            //credit the member
            DAL.EmployeeSignOut(signIn);
            //clear the daily signin 
            DAL.CancelSignin(data);
            //set the last used for the card
            DAL.SetIDCardLastUsedDate(data);
        }

        public void CloseDailySignins(ScanRequest data)
        {
            //get the current signins
            Patrol patrol = new Patrol();
            patrol.dbPrefix = data.DataTransaction.PatrolPrefix;
            List<ScanResponse> signins = GetDailySignins(patrol);

            foreach (ScanResponse item in signins)
            {
                item.DataTransaction = data.DataTransaction;
                item.ScanDateTime = data.ScanDateTime;
                item.SignOut = item.ScanDateTime;
                this.EmployeeSignOut(item);
            }
        }        
    }
}
